home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Libris Britannia 4
/
science library(b).zip
/
science library(b)
/
PROGRAMM
/
DB_CLIPP
/
0277.ZIP
/
WILDCP86.C
< prev
next >
Wrap
Text File
|
1986-02-28
|
4KB
|
224 lines
/*
WILDEXP1.C - CP/M86 CI86 C version. Thanks to L. Zolman for the original
BDS C version.
*/
#include <stdio.h>
#define MAXITEMS 200 /* max no. of items after expansion */
#define SEARCH_FIRST 17 /* BDOS calls */
#define SEARCH_NEXT 18
#define ERROR -1
#define TRUE 1
#define FALSE 0
wildexp(oargcp, oargvp)
int *oargcp; /* pointer to old argc */
char ***oargvp; /* pointer to old argv */
{
int nargc; /* new argc */
char **nargv; /* new argv */
char **oargv; /* old argv */
int oargc; /* old argc */
char fcb[36]; /* fcb used for search for first/next calls */
char dmapos; /* value returned by search calls */
char first_time; /* used in search routine */
char tmpfn[20], /* temp filename buffer */
*tmpfnp;
char *notfns[20]; /* list of !<afn> entries */
int notcount; /* count of entries in notfns */
char cur_drive; /* currently logged drive */
int i,j,k;
char *sbrk();
char dma[128];
struct { unsigned int scs, sss, sds, ses;} segs;
struct { unsigned int ax,bx,cx,dx,si,di,ds,es;} srvi, srvo;
cur_drive = bdos(25);
segread(&segs);
bdos(0x1a,(unsigned) &dma[0]); /* set dma offset */
bdos(0x33,segs.sds); /* set dma segment */
oargv = *oargvp;
oargc = *oargcp;
nargc = 1;
notcount = 0;
if ((nargv = sbrk(MAXITEMS * 2 + 2)) == NULL)
return ERROR;
for (i = 1; i < oargc; i++)
{
upper(oargv[i]);
if (oargv[i][0] == '!')
{
if (i == 1)
{
oargv[oargc] = "*.*";
oargc++;
}
notfns[notcount++] = &oargv[i][1];
}
else if (!haswild(oargv[i]))
nargv[nargc++] = oargv[i];
else
{
setfcb(fcb,oargv[i]);
tmpfnp = tmpfn;
if ((tmpfn[1] = oargv[i][1]) == ':')
{
tmpfn[0] = toupper(oargv[i][0]);
tmpfnp = tmpfn + 2;
}
first_time = TRUE;
while (1)
{ /* find all matching files */
srvi.cx = (first_time) ? SEARCH_FIRST : SEARCH_NEXT;
srvi.ds = segs.sds;
srvi.dx = (unsigned) &fcb[0];
sysint(224, &srvi, &srvo);
if((srvo.ax & 0x00ff) == 255)
break;
dmapos = srvo.ax & 0x00ff;
first_time = FALSE;
hackname(tmpfnp,dma+(dmapos*32));
if ((nargv[nargc] = sbrk(strlen(tmpfn) + 1)) == NULL)
return ERROR;
strcpy(nargv[nargc++], tmpfn);
}
}
}
for (i = 0; i < notcount; i++)
for (j = 1; j < nargc; j++)
while (match(notfns[i],nargv[j],cur_drive))
{
if(j == --nargc)
break;
for (k = j; k < nargc; k++)
nargv[k] = nargv[k+1];
}
*oargcp = nargc;
*oargvp = nargv;
return (0);
}
hackname(dest,source)
char *dest, *source;
{
int i,j;
j = 0;
for (i = 1; i < 9; i++)
{
if (source[i] == ' ') break;
dest[j++] = source[i];
}
if (source[9] != ' ')
dest[j++] = '.';
for (i = 9; i < 12; i++)
{
if (source[i] == ' ') break;
dest[j++] = source[i];
}
dest[j] = '\0';
return dest;
}
int haswild(fname)
char *fname;
{
char c;
while (c = *fname++)
if (c == '*' || c == '?')
return TRUE;
return FALSE;
}
int match(wildnam, filnam, cur_drive)
char *wildnam, *filnam, cur_drive;
{
char c;
if (wildnam[1] != ':')
{
if (filnam[1] == ':')
if (filnam[0] - 'A' == cur_drive)
filnam += 2;
else
return FALSE;
}
else
{
if (filnam[1] != ':')
if (wildnam[0] - 'A' == cur_drive)
wildnam += 2;
else
return FALSE;
}
while (c = *wildnam++)
if (c == '?')
if ((c = *filnam++) && c != '.')
continue;
else
return FALSE;
else if (c == '*')
{
while (c = *wildnam)
{ wildnam++;
if (c == '.') break;
}
while (c = *filnam)
{ filnam++;
if (c == '.') break;
}
}
else if (c == *filnam++)
continue;
else return FALSE;
if (!*filnam)
return TRUE;
else
return FALSE;
}
setfcb(fcb,fname)
char *fcb, *fname;
{
char *fcb_ptr, *makefcb();
int i;
int qflg =0;
if((fcb_ptr = makefcb(fname)) == NULL)
return(0);
movmem(fcb_ptr, fcb, 36);
free(fcb_ptr);
for(i=1; i<12;i++)
{
if(fcb[i] == '*')
{
qflg = 1;
fcb[i] = '?';
continue;
}
if(qflg)
{
if(fcb[i] != ' ')
{
qflg =0;
continue;
}
fcb[i] = '?';
}
}
return(1);
}